package com.bainuo.doctor.common.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bainuo.doctor.common.R;
import com.bainuo.doctor.common.util.ToastUtils;
import com.bainuo.doctor.common.widget.CustomToolbar;
import com.bainuo.doctor.common.widget.LoadingDialog;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Description:使用 butterknife，重写抽象方法  基础类fragment
 * Created by MonkeyShen on 2017/1/17.
 * Mail:shenminjie92@sina.com
 */

public abstract class BaseFragment extends Fragment implements BaseView , CustomToolbar.OnToolbarClickListener {

    /**
     * 解绑
     */
    private Unbinder mUnbinder;

    /**
     * 加载中
     */
    private LoadingDialog mLoadingDialog;


    /**
     * mContainer
     */
    private View mContainer;

    private CustomToolbar mToolbar;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLoadingDialog = new LoadingDialog(getActivity());
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //不要随便改动，实现onCreateContianerView即可
        mContainer = onCreateFragmentView(inflater, container, savedInstanceState);
        mUnbinder = ButterKnife.bind(this, mContainer);
        return mContainer;
    }

    public View createWithToolbar(ViewGroup container,int contentView){
        ViewGroup rootView = (ViewGroup)getLayoutInflater().inflate(R.layout.layout_header_fragment, container, false);
        View view = getLayoutInflater().inflate(contentView, null, false);
        ViewGroup viewGroup = ((ViewGroup) rootView).findViewById(com.bainuo.doctor.common.R.id.root_content_view);
        viewGroup.addView(view);
        init(rootView);
        return rootView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    @Override
    public void onDestroyView() {
        //处理加载中操作
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
        mUnbinder.unbind();
        super.onDestroyView();
    }

    /**
     * 获取view
     * 在其中封装butterknife代码
     * 使用者只需要实现此方法，即可，使用butterknife会处理绑定
     *
     * @param inflater           inflater
     * @param container          container
     * @param savedInstanceState savedInstanceState
     * @return
     */
    public abstract View onCreateFragmentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);


    @Override
    public abstract void initView();

    @Override
    public void showLoading() {
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
        }
    }

    @Override
    public void showLoading(String text) {
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show(text);
        }
    }

    /**
     * dialog  点击其他地方不消失
     * @param isCance false
     */
    public void showLoading(boolean isCance){
        if (!mLoadingDialog.isShowing()) {
            mLoadingDialog.show();
            mLoadingDialog.setCanceledOnTouchOutside(isCance);
            mLoadingDialog.setCancelable(isCance);
        }
    }

    @Override
    public void hideLoading() {
        if (mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @Override
    public void showToast(CharSequence text) {
        ToastUtils.showToast(text);
    }

    /**
     * 设置友盟统计点击事件
     * @param evnetId  事件iD
     */
    public void setUmengEvent(String evnetId){
        MobclickAgent.onEvent(getActivity(), evnetId);
    }




    /**
     * 初始化toolbar
     */
    private void init(ViewGroup viewGroup) {
        mToolbar = (CustomToolbar)viewGroup.findViewById(R.id.toolbar);
        if (mToolbar != null) {
            //设置toolbar基础，监听事件、按钮显示
            mToolbar.setListener(this);
//            setSupportActionBar(mToolbar);
//            mToolbar.isShowLeftIcon(true);
//            mToolbar.setMainTitleLeftDrawable(R.mipmap.icon_fanhui);
        }
        hideToolbar();
    }

    public CustomToolbar getToolbar() {
        return mToolbar;
    }

    public void hideToolbar() {
        if (mToolbar != null)
            mToolbar.setVisibility(View.GONE);
    }
    public void showToolbar() {
        if (mToolbar != null)
            mToolbar.setVisibility(View.VISIBLE);
    }



    public void onLeftIconClickListener(View leftView){};

    /**
     * 右icon点击事件
     *
     * @param rightView rightView
     */
    public void onRightIconClickListener(View rightView){};

    /**
     * 主title点击事件处理
     *
     * @param titleView titleView
     */
    public void onMainTitleClickListener(View titleView){};

    /**
     * 右边还有一个icon
     * @param view
     */
    public void onRightOtherClickListener(View view){};
}
